package com.ruoyi.voucher;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.voucher.domain.Subject;
import com.ruoyi.voucher.domain.Summary;
import com.ruoyi.voucher.domain.VoucherWord;
import com.ruoyi.voucher.mapper.SubjectMapper;
import com.ruoyi.voucher.mapper.SummaryMapper;
import com.ruoyi.voucher.mapper.VoucherWordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: RuoYi-Cloud-master
 * @BelongsPackage: com.ruoyi.voucher
 * @Author: songYuHang
 * @CreateTime: 2025-02-25  09:53
 * @Description: TODO
 * @Version: 1.0
 */
@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class RuoYiVoucherApplication {

    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiVoucherApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  账套模块模块启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    @Autowired
    private VoucherWordMapper wordMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SummaryMapper summaryMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 凭证字缓存键
    private static final String WORD_KEY = "word:wordList";
    // 科目缓存键
    private static final String SUBJECT_KEY = "subject:subjectList";
    // 摘要缓存键
    private static final String SUMMARY_KEY = "summary:summaryList";


    @PostConstruct
    public void loadData()
    {
        // 存储之前，先清空一下
        redisTemplate.delete(Arrays.asList(WORD_KEY, SUBJECT_KEY, SUMMARY_KEY));

        List<VoucherWord> voucherWords = wordMapper.selectList(null);
        List<Subject> subjects = subjectMapper.selectList(null);
        List<Summary> summaries = summaryMapper.selectList(null);

        redisTemplate.opsForValue().set(WORD_KEY, JSON.toJSONString(voucherWords));
        redisTemplate.opsForValue().set(SUBJECT_KEY, JSON.toJSONString(subjects));
        redisTemplate.opsForValue().set(SUMMARY_KEY, JSON.toJSONString(summaries));

    }

}
